<?php if (!defined('CORE_DIR')) die('Wrong entry point!');

/* 
 * Copyright 2010 Anton Muraviev a.k.a kuromaisu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

Aero::define('DOMLibrarySpecification', AS_SINGLETON);

/*
 * SpecificationLibarySpecification
 */
class DOMLibrarySpecification extends AeroSpec
{
	protected function itShouldLoadAnHTMLDocument()
	{
		$html = '<html><body><h1>This is an almost valid HTML document!</h1></body></html>';
		
		$dom = Aero::load('libs/parsers/dom');
		$dom->loadHTML($html);
		$this->value($dom->getElementsByTagName('h1')->length)->equals(1);
	}
	
	protected function itShouldLoadAnHTMLDocumentViaConstructor()
	{
		$html = '<html><body><h1>This is an almost valid HTML document!</h1></body></html>';
		
		$dom = Aero::load('libs/parsers/dom',array('html' => $html));
		
		$this->object($dom)->isInstanceOf('AeroDOMDocument');
		$this->value($dom->getElementsByTagName('h1')->length)->equals(1);
	}
	
	protected function itShouldLoadAnInvalidHTMLDocumentWithoutThrowingAnyErrors()
	{
		$invalid_html = '<html><body><foo id="lada-kalina"></foo></body>';
		
		$dom = Aero::load('libs/parsers/dom',array('html' => $invalid_html));
		
		$errors = $dom->documentErrors(E_WARNING);
		$this->vector($errors)->hasItemAt(0);
		
		$this->value($dom->getElementsByTagName('body')->length)->equals(1);
		$this->value($dom->getElementById('lada-kalina'))->isNot('null');
	}
	
	protected function itShouldNotAttemptToFixTheDocument()
	{
		$invalid_html = '<html><body class="body"><h1>Love</h1><div>suck</div></body></html>';
		
		$dom = Aero::load('libs/parsers/dom',array('html' => $invalid_html));
		
		$this->value($dom->getElementsByTagName('div')->length)->equals(1);
		$this->value($dom->getElementsByTagName('p')->length)->equals(0);
	}
	
	protected function itShouldPerformXPATHQueries()
	{
		$invalid_html = '<html><body class="body"><h1>Love</h1><div>suck</div></body></html>';
		
		$dom = Aero::load('libs/parsers/dom',array('html' => $invalid_html));
		
		$this->value($dom->getElementsByXPath('//html/*[@class = "body"]/h1')->length)->equals(1);
	}
	
	private $sample_html = '
		<html>
		<head>
		<meta http-equiv="Content-Type" content=" text/html; charset=utf-8"/>
		<title>This is a very basic page</title>
		</head>
		<body>
		
		<div id="container">
		    <div id="header">
		        <img src="mylogo.png" id="logo"/>
		    </div>
			
		    <div id="contents" class="box anotherclass">
		        <h2>This page is cool</h2>
		        <p>This page should be XHTML valid because : </p>
		        <ul>
		            <li class="box">It respects standarts</li>
		            <li>It\'s written by some dude on the internet</li>
		            <li>It\'s simple</li>
		        </ul>
				<form action="dom_spec.php" method="post" accept-charset="utf-8">
					<p>Check this box: <label><input type="checkbox" name="check_1" value="1" /> Checkbox 1</label></p>
					<p>But not this one: <label><input type="checkbox" name="check_2" value="1" checked="checked" /> Checkbox 2</label></p>
					<p>
						Radio: 
						<label><input type="radio" name="group_1" value="1" disabled="disabled" /> Radio 1</label>
						<label><input type="radio" name="group_1" value="2" checked="checked" /> Radio 2</label>
						<label><input type="radio" name="group_1" value="3" /> Radio 3</label>
					</p>

					<p><input type="submit" value="Continue &rarr;"></p>
				</form>
		    </div>
			
		<p>This is <i>a test <u>of serialisation</u> for <code>$this-&gt;get("text")</code></i>, another node</p>
		
		</div>
		
		</body>
		</html>';
	
	protected function itShouldPerformCSSSelectorQueries()
	{
		$dom = Aero::load('libs/parsers/dom',array('html' => $this->sample_html));
		
		$this->value($dom->getElementsBySelector('body')->length)->equals(1);
		$this->value($dom->getElementsBySelector('html body')->length)->equals(1);
		$this->value($dom->getElementsBySelector('html body body')->length)->equals(0);
		$this->value($dom->getElementsBySelector('html body div')->length)->equals(3);
		$this->value($dom->getElementsBySelector('html>body>div')->length)->equals(1);
		$this->value($dom->getElementsBySelector('html>div')->length)->equals(0);
		$this->value($dom->getElementsBySelector('html#container')->length)->equals(0);
		$this->value($dom->getElementsBySelector('#container')->length)->equals(1);
		$this->value($dom->getElementsBySelector('div#header li')->length)->equals(0);
		$this->value($dom->getElementsBySelector('.box')->length)->equals(2);
		$this->value($dom->getElementsBySelector('.box.anotherclass')->length)->equals(1);
		$this->value($dom->getElementsBySelector('*[content*=" text/html; "]')->length)->equals(1);
		$this->value($dom->getElementsBySelector('li~li')->length)->equals(2);
		$this->value($dom->getElementsBySelector('.box+li')->length)->equals(1);
		$this->value($dom->getElementsBySelector('li:first-child')->length)->equals(1);
		$this->value($dom->getElementsBySelector('li:last-child')->length)->equals(1);
		$this->value($dom->getElementsBySelector('#header img:only-child')->length)->equals(1);
		$this->value($dom->getElementsBySelector('#header img:empty')->length)->equals(1);
		$this->value($dom->getElementsBySelector('input[type=radio]:checked')->length)->equals(1);
		$this->value($dom->getElementsBySelector('input[type=radio]:disabled')->length)->equals(1);
		$this->value($dom->getElementsBySelector('input[type=radio]:enabled')->length)->equals(2);
	}
}